<?php
/**
 * YAPP Base Model class
 *
 */

class Model
{

    // Database
    protected $db;
    protected $session;

    // misc
    protected $noticesKey = '__YAPP__NOTICES__SESSION__KEY';

    // Constructor
    public function __construct()
    {
        $this->db = genericDBWrapper::getInstance();
        $this->session = yappSession::getInstance();
    }

    // Add notice to session
    public function notice($msg)
    {
        $session = yappSession::getInstance();
        $notices = $session->get($this->noticesKey);
        if (empty($notices))
        {
            $notices = array();
        }
        if (is_array($msg))
        {
            foreach($msg as $m) $notices[] = array('type' => MESSAGE_TYPE_NOTICE, 'message' => $m);
        }
        else
        {
            $notices[] = array('type' => MESSAGE_TYPE_NOTICE, 'message' => $msg);
        }
        $session->set($this->noticesKey, $notices);
    }

    // Add error to session
    public function error($msg)
    {
        $session = yappSession::getInstance();
        $notices = $session->get($this->noticesKey);
        if (empty($notices))
        {
            $notices = array();
        }
        if (is_array($msg))
        {
            foreach($msg as $m) $notices[] = array('type' => MESSAGE_TYPE_ERROR, 'message' => $m);
        }
        else
        {
            $notices[] = array('type' => MESSAGE_TYPE_ERROR, 'message' => $msg);
        }
        $session->set($this->noticesKey, $notices);
    }

    // Get notices from session and empty array
    public function getNotices()
    {
        $session = yappSession::getInstance();
        $notices = $session->get($this->noticesKey);
        $session->set($this->noticesKey, false);
        return $notices;
    }

    // Escape using mysql real escape string
    public function escape($string)
    {
        return $this->db->escape($string);
    }

}
?>